package com.vineetmanohar.nitrogen;


/**
 * A convenience class which can be used to crawl a model
 * 
 * @author vineetmanohar
 * 
 */
public class ModelCrawler {
	public static void crawl(Model model, CrawlListener crawlListener) {
		crawlListener.modelFound(model);

		if (model.isPrimitive()) {
			PrimitiveModel primitiveModel = model.getPrimitive();
			crawlListener.primitiveModelFound(primitiveModel);
		} else if (model.isObject()) {
			ObjectModel objectModel = model.getObjectModel();
			crawlListener.objectModelFound(objectModel);

			for (String childName : objectModel.keySet()) {
				Model child = objectModel.get(childName);
				crawl(child, crawlListener);
			}
		} else if (model.isArray()) {
			ArrayModel arrayModel = model.getArrayModel();
			crawlListener.arrayModelFound(arrayModel);
			for (Model child : arrayModel) {
				crawl(child, crawlListener);
			}
		}
	}
}